package com.ratchet.ShotShakr;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class ShotRepository {

	   private static final String DATABASE_NAME = "shotshakr.db";
	   private static final int DATABASE_VERSION = 2;
	   private static final String TABLE_NAME = "shot";

	   private Context context;
	   private SQLiteDatabase db;
	   private OpenHelper openHelper;

	   private SQLiteStatement insertStmt;
	   private static final String INSERT = "insert into " 
	      + TABLE_NAME + "(name, ingredients) values (?,?)";
	   private static final String WHERE_NAME = "";

	   public ShotRepository(Context context) {
	      this.context = context;
	      openHelper = new OpenHelper(this.context);
	      this.db = openHelper.getWritableDatabase();
	      this.insertStmt = this.db.compileStatement(INSERT);
	   }

	   public long insert(String name, String ingredients) {
	      this.insertStmt.bindString(1, name);
	      this.insertStmt.bindString(2, ingredients);
	      return this.insertStmt.executeInsert();
	   }

	   public void deleteAll() {
	      this.db.delete(TABLE_NAME, null, null);
	   }

	   public void close() {
		   if (openHelper != null)
			   openHelper.close();
		   
		   this.db.close();
	   }
	   public ArrayList<Shot> getAllShots() {
		   ArrayList<Shot> list = new ArrayList<Shot>();
	      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name", "ingredients" }, 
	        null, null, null, null, "name desc");
	      if (cursor.moveToFirst()) {
	         do {
	        	 Shot shot = new Shot(cursor.getString(0), cursor.getString(1));
	        	 
	            list.add(shot); 
	         } while (cursor.moveToNext());
	      }
	      if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	      }
	      return list;
	   }
	   
	   private static class OpenHelper extends SQLiteOpenHelper {

	      OpenHelper(Context context) {
	         super(context, DATABASE_NAME, null, DATABASE_VERSION);
	      }

	      @Override
	      public void onCreate(SQLiteDatabase db) {
	         db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT, ingredients TEXT)");
	      }

	      @Override
	      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
	         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
	         onCreate(db);
	      }
	   }
	   
	   public class Shot {
	   
		   private String _shotName;
		   private String _shotIngredients;
		   public String getShotName() {
				return _shotName;
			}

			public String getIngredients() {
				return _shotIngredients;
			}
			public void setShotName(String value) {
				_shotName = value;
			}

			public void setIngredients(String value) {
				_shotIngredients = value;
			}
			
			public Shot(String name, String i) {
				setShotName(name);
				setIngredients(i);
			}
			

	   
	   }
	
}
